package com.qixin.modules.eaApplication.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.qixin.modules.ea.entity.EaSysDictItem;
import com.qixin.modules.ea.mapper.EaSysDictItemMapper;
import com.qixin.modules.ea.mapper.EaSysUserMapper;
import com.qixin.modules.ea.vo.ListPageVo;
import com.qixin.modules.eaApplication.entity.*;
import com.qixin.modules.eaApplication.mapper.EaBusinessApplyMapper;
import com.qixin.modules.eaApplication.mapper.EaBusinessApplyChangeMapper;
import com.qixin.modules.eaApplication.mapper.EaBusinessPerDtlMapper;
import com.qixin.modules.eaApplication.service.IEaBusinessApplyChangeServiceCopy;
import com.qixin.modules.eaBasic.entity.EaTask;
import com.qixin.modules.eaBasic.mapper.EaTaskMapper;
import com.qixin.modules.eaBasic.util.ChangeIdToNameUtil;
import com.qixin.modules.eaBasic.util.WorkFlowUtil;
import com.qixin.modules.eaBasic.vo.EaTaskVo;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: 出差申请变更单
 * @Author: qixin-boot
 * @Date:   2021-07-02
 * @Version: V1.0
 */
@Service
public class EaBusinessApplyChangeServiceImplCopy extends ServiceImpl<EaBusinessApplyChangeMapper, EaBusinessApplyChange> implements IEaBusinessApplyChangeServiceCopy {

	@Autowired
	private EaBusinessApplyChangeMapper eaBusinessApplyChangeMapper;
	@Autowired
	private EaTaskMapper eaTaskMapper;
	@Autowired
	private ChangeIdToNameUtil changeIdToNameUtil;
	@Autowired
	private EaSysUserMapper eaSysUserMapper;
	@Autowired
	private EaBusinessApplyMapper eaBusinessApplyMapper;
	@Autowired
	private WorkFlowUtil workFlowUtil;
	@Autowired
	private EaBusinessPerDtlMapper eaBusinessPerDtlMapper;
	@Autowired
	private EaSysDictItemMapper eaSysDictItemMapper;

	@Value(value = "${server.fileurl}")
	private String fileurl;

	//新增出差申请变更单
	@Override
	@Transactional
	public Map<String,Object> saveMain(EaBusinessApplyChange eaBusinessApplyChange,List<EaBusinessPerDtl> eaBusinessPerDtlList) {
		Map<String,Object> returnMap = new HashMap<String,Object>();
		Object result = FillRuleUtil.executeRule("BusinessApplyChangeCodeRule", null);  //获取单据编号
		eaBusinessApplyChange.setBillNo(result.toString());

		String saveType = eaBusinessApplyChange.getSaveType();
		if(saveType.equals("add")) {  //add保存,send提交
			eaBusinessApplyChange.setState(0);   //单据状态:0未提交,1审批中,2已审批,3已退回
		}else{
			eaBusinessApplyChange.setState(1);   //单据状态:0未提交,1审批中,2已审批,3已退回
		}

		eaBusinessApplyChange.setId(Uuid32Util.getUUid());  //主键
		eaBusinessApplyChangeMapper.insert(eaBusinessApplyChange);

		//保存出行人员明细
		if(eaBusinessPerDtlList != null && eaBusinessPerDtlList.size() > 0){
			for (int i = 0; i < eaBusinessPerDtlList.size(); i++) {
				EaBusinessPerDtl eaBusinessPerDtl = eaBusinessPerDtlList.get(i);
				eaBusinessPerDtl.setId(Uuid32Util.getUUid());
				eaBusinessPerDtl.setEaBusinessApplyId(eaBusinessApplyChange.getId());
				eaBusinessPerDtlMapper.insert(eaBusinessPerDtl);
			}
		}

//		//新增待办
//		EaTask eaTask = new EaTask();
//		eaTask.setId(Uuid32Util.getUUid());
//		eaTask.setFormType("5");  //单据类型:1差旅费报销单,2借款单,3还款单,4出差申请单,5出差申请变更单,6招待费申请单,7招待费申请变更单,8招待费报销单,9资金支付审批单,10通讯报销单,11电话记录单
//		eaTask.setAuditManId(eaBusinessApplyChange.getAuditManId());   //审批人id
//		eaTask.setAuditMan(eaBusinessApplyChange.getAuditMan());    //审批人
//		eaTask.setFormId(eaBusinessApplyChange.getId());    //单据id
//		eaTask.setFormCode(eaBusinessApplyChange.getBillNo());   //单据编号
//		eaTask.setMemo("");    //备注
//
		//根据出差申请单编号去查询出差申请单,获取相应的总金额
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("bill_no",eaBusinessApplyChange.getEaBusinessApplyNo());
		List<EaBusinessApply> eaBusinessApplyList = eaBusinessApplyMapper.selectByMap(params);
		if(eaBusinessApplyList != null && eaBusinessApplyList.size() > 0){
			EaBusinessApply eaBusinessApply = eaBusinessApplyList.get(0);
			if(eaBusinessApply.getIfChanged() == 1){
				throw new RuntimeException("出差申请单:" + eaBusinessApply.getBillNo() + "已申请变更,请选择其他单据!");
			}

			eaBusinessApply.setIfChanged(1);     //是否变更状态
			eaBusinessApplyMapper.updateById(eaBusinessApply);
//			eaTask.setTotalFee(eaBusinessApply.getApplyMoney()); //总金额
		}
//
//		eaTask.setSort("5");   //排序
//		eaTask.setTitle("出差申请变更单");  //标题
//		eaTask.setSendManId(eaBusinessApplyChange.getPersonId());    //发送人id
//		eaTask.setSendMan(changeIdToNameUtil.getPsnNameById(eaBusinessApplyChange.getPersonId()));    //发送人
//
//		//根据用户id获取用户部门
//		EaSysUser eaSysUser = eaSysUserMapper.selectById(eaBusinessApplyChange.getPersonId());
//
//		if(eaSysUser != null){
//			eaTask.setSendDepartId(eaSysUser.getDepartIds());  //发送人部门id
//			eaTask.setSendDepart(changeIdToNameUtil.getNameByDepartId(eaSysUser.getDepartIds()));   //发送人部门
//		}
//		eaTask.setSendTime(new Date());    //发送时间
//		//提交类型:保存:add,提交:send
//		if(saveType.equals("add")){  //add保存,send提交
//			eaTask.setStatus("0");
//		}else{
//			eaTask.setStatus("1");
//		}
//		eaTaskMapper.insert(eaTask);

		returnMap.put("id",eaBusinessApplyChange.getId());
		returnMap.put("code","0");
		returnMap.put("message","保存成功!");
		return returnMap;
	}

	//编辑出差申请变更单
	@Override
	@Transactional
	public Map<String,Object> updateMain(EaBusinessApplyChange eaBusinessApplyChange,List<EaBusinessPerDtl> eaBusinessPerDtlList) {
		Map<String,Object> returnMap = new HashMap<String,Object>();

		//如果picture中有http://localhost:9092/qixin-boot/aaa.png,要处理只保留文件名
		String picture = eaBusinessApplyChange.getPicture() != null ? eaBusinessApplyChange.getPicture().toString() : "";
		String picture_final = picture.replaceAll(fileurl,"");;
		//设置最终的图片如:1111.png,2222.png
		eaBusinessApplyChange.setPicture(picture_final);

		String saveType = eaBusinessApplyChange.getSaveType();
		if(saveType.equals("add")) {  //add保存,send提交
			EaBusinessApplyChange eaBusinessApplyChange1 = eaBusinessApplyChangeMapper.selectById(eaBusinessApplyChange.getId());
			eaBusinessApplyChange.setState(eaBusinessApplyChange1.getState());   //单据状态:0未提交,1审批中,2已审批,3已退回
		}else{
			eaBusinessApplyChange.setState(1);   //单据状态:0未提交,1审批中,2已审批,3已退回
		}

		//将旧的关联申请单变更状态改成0
		EaBusinessApplyChange eaBusinessApplyChange3  = eaBusinessApplyChangeMapper.selectById(eaBusinessApplyChange.getId());
		Map<String,Object> params3 = new HashMap<String,Object>();
		params3.put("bill_no",eaBusinessApplyChange3.getEaBusinessApplyNo());
		List<EaBusinessApply> eaBusinessApplyList3 = eaBusinessApplyMapper.selectByMap(params3);
		if(eaBusinessApplyList3 != null && eaBusinessApplyList3.size() > 0){
			EaBusinessApply eaBusinessApply = eaBusinessApplyList3.get(0);
			eaBusinessApply.setIfChanged(0);     //是否变更状态
			eaBusinessApplyMapper.updateById(eaBusinessApply);
		}

		eaBusinessApplyChangeMapper.updateById(eaBusinessApplyChange);

		//删除出现人员明细
		Map<String,Object> params2 = new HashMap<String,Object>();
		params2.put("ea_business_apply_id",eaBusinessApplyChange.getId());
		eaBusinessPerDtlMapper.deleteByMap(params2);

		//再保存出行人员明细表
		if(eaBusinessPerDtlList != null && eaBusinessPerDtlList.size() > 0){
			for (int i = 0; i < eaBusinessPerDtlList.size(); i++) {
				EaBusinessPerDtl eaBusinessPerDtl = eaBusinessPerDtlList.get(i);
				eaBusinessPerDtl.setId(Uuid32Util.getUUid());
				eaBusinessPerDtl.setEaBusinessApplyId(eaBusinessApplyChange.getId());
				eaBusinessPerDtlMapper.insert(eaBusinessPerDtl);
			}
		}

		//修改待办
//		Map<String,Object> paramsMap2 = new HashMap<String,Object>();
//		paramsMap2.put("form_id",eaBusinessApplyChange.getId());
//		List<EaTask> eaTaskList = eaTaskMapper.selectByMap(paramsMap2);
//		for (EaTask eaTask:eaTaskList) {
//			eaTask.setAuditManId(eaBusinessApplyChange.getAuditManId());   //审批人id
//			eaTask.setAuditMan(eaBusinessApplyChange.getAuditMan());    //审批人
//			eaTask.setFormId(eaBusinessApplyChange.getId());    //单据id
//			eaTask.setFormCode(eaBusinessApplyChange.getBillNo());   //单据编号
//			eaTask.setMemo("");    //备注
//
//			//根据出差申请单编号去查询出差申请单,获取相应的总金额
//			Map<String,Object> params = new HashMap<String,Object>();
//			params.put("bill_no",eaBusinessApplyChange.getEaBusinessApplyNo());
//			List<EaBusinessApply> eaBusinessApplyList = eaBusinessApplyMapper.selectByMap(params);
//			if(eaBusinessApplyList != null && eaBusinessApplyList.size() > 0){
//				EaBusinessApply eaBusinessApply = eaBusinessApplyList.get(0);
//				if(eaBusinessApply.getIsChanging().equals("1")){
//					throw new RuntimeException("出差申请单:" + eaBusinessApply.getBillNo() + "已申请变更,请选择其他单据!");
//				}
//
//				eaBusinessApply.setIsChanging("1");     //是否变更状态
//				eaBusinessApplyMapper.updateById(eaBusinessApply);
//				eaTask.setTotalFee(eaBusinessApply.getApplyMoney()); //总金额
//			}
//
//			eaTask.setSendManId(eaBusinessApplyChange.getPersonId());    //发送人id
//			eaTask.setSendMan(changeIdToNameUtil.getPsnNameById(eaBusinessApplyChange.getPersonId()));    //发送人
//
//			//根据用户id获取用户部门
//			EaSysUser eaSysUser = eaSysUserMapper.selectById(eaBusinessApplyChange.getPersonId());
//
//			if(eaSysUser != null){
//				eaTask.setSendDepartId(eaSysUser.getDepartIds());  //发送人部门id
//				eaTask.setSendDepart(changeIdToNameUtil.getNameByDepartId(eaSysUser.getDepartIds()));   //发送人部门
//			}
//			eaTask.setSendTime(new Date());    //发送时间
//			//提交类型:保存:add,提交:send
//			if(saveType.equals("add")){  //add保存,send提交
//				eaTask.setStatus("0");
//			}else{
//				eaTask.setStatus("1");
//			}
//			eaTaskMapper.updateById(eaTask);
//		}

		returnMap.put("id",eaBusinessApplyChange.getId());
		returnMap.put("code","0");
		returnMap.put("message","编辑成功!");
		return returnMap;
	}

	@Override
	@Transactional
	public void delMain(String id) {
		EaBusinessApplyChange eaBusinessApplyChange = eaBusinessApplyChangeMapper.selectById(id);

		//将关联的申请单变更状态变成0
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("bill_no",eaBusinessApplyChange.getEaBusinessApplyNo());
		List<EaBusinessApply> eaBusinessApplyList = eaBusinessApplyMapper.selectByMap(params);
		if(eaBusinessApplyList.size() > 0){
			for (EaBusinessApply eaBusinessApply: eaBusinessApplyList) {
				eaBusinessApply.setIfChanged(0);      //未变更状态
				eaBusinessApplyMapper.updateById(eaBusinessApply);
			}
		}

		eaBusinessApplyChangeMapper.deleteById(id);

		//删除相应的待办
//		Map<String,Object> paramaMap = new HashMap<String,Object>();
//		paramaMap.put("form_id",id);
//		List<EaTask> eaTaskList = eaTaskMapper.selectByMap(paramaMap);
//		if(eaTaskList != null && eaTaskList.size() > 0){
//			EaTask eaTask = eaTaskList.get(0);
//
//			//删除相应的工作流
//			workFlowUtil.deleteWorkFlowById(eaTask.getWorkflowTaskId(),eaTask.getProcinstId());
//
//			eaTaskMapper.deleteById(eaTask.getId());
//		}
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			eaBusinessApplyChangeMapper.deleteById(id);
		}
	}

	//查询出差申请单列表
	@Override
	public Map<String, Object> list(ListPageVo listPageVo) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		Map<String,Object> map = new HashMap<String,Object>();
		String page = EmptyUtil.checkNull(listPageVo.getPage());
		String limit = EmptyUtil.checkNull(listPageVo.getLimit());
		String keyword = EmptyUtil.checkNull(listPageVo.getKeyword());
		String userid = EmptyUtil.checkNull(loginUser.getId());   //用户id
		String status = EmptyUtil.checkNull(listPageVo.getStatus());   //状态
		String billNo = EmptyUtil.checkNull(listPageVo.getBillNo());   //单据编号(pc)
		String billDateBegin = EmptyUtil.checkNull(listPageVo.getBillDateBegin());   //单据日期-开始(pc)
		String billDateEnd = EmptyUtil.checkNull(listPageVo.getBillDateEnd());   //单据日期-结束(pc)
		String personId = EmptyUtil.checkNull(listPageVo.getPersonId());   //申请人/报销人/借款人/还款人(pc)
		String explainStr = EmptyUtil.checkNull(listPageVo.getCasue());   //事由(pc)
		String sysOrgCode = EmptyUtil.checkNull(listPageVo.getSysOrgCode());   //所属组织(pc)
		String keyword1 = "%" + keyword + "%";
		map.put("keyword",keyword1);
		map.put("page",(Integer.parseInt(page) -1) * Integer.parseInt(limit));
		map.put("limit",Integer.parseInt(limit));
		map.put("userid",userid);
		map.put("status",status);
		map.put("billNo",billNo);
		map.put("billDateBegin",billDateBegin);
		map.put("billDateEnd",billDateEnd);
		map.put("personId",personId);
		map.put("explainStr",explainStr);
		map.put("sysOrgCode",sysOrgCode);
		List<EaBusinessApplyChange> eaBusinessApplyChangeList = eaBusinessApplyChangeMapper.selectBusinessApplyChanges(map);
		if(eaBusinessApplyChangeList != null && eaBusinessApplyChangeList.size() > 0){
			for (EaBusinessApplyChange eaBusinessApplyChange:eaBusinessApplyChangeList) {

				//更新时间-用于校验时效性
				Date date = eaBusinessApplyChange.getUpdateTime();
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String time = "";   //单据的最新更新时间
				try {
					time = simpleDateFormat.format(date);
				}catch (Exception e){
					time = "";
				}
				eaBusinessApplyChange.setTime(time);
			}
		}

		int count = 0;
		map.put("page",0);
		map.put("limit",0);
		map.put("keyword",keyword1);
		map.put("userid",userid);
		List<EaBusinessApplyChange> list2 = eaBusinessApplyChangeMapper.selectBusinessApplyChanges(map);
		count = list2.size();
		Map<String,Object> returnMap = new HashMap<String,Object>();
		returnMap.put("records",eaBusinessApplyChangeList);
		returnMap.put("total",count);
		return returnMap;
	}

	//查询出差申请变更单详情
	@Override
	public Map<String, Object> selectById(String id) {
		//查询出差申请变更单详情
		Map<String,Object> queryWrapper = new HashMap<String,Object>();
		queryWrapper.put("id",id);  //出差申请变更单id
		queryWrapper.put("page",0);
		queryWrapper.put("limit",0);
		List<EaBusinessApplyChange> eaBusinessApplyChangeList = eaBusinessApplyChangeMapper.selectBusinessApplyChanges(queryWrapper);
		Map<String,Object> returnMap = new HashMap<String,Object>();
		if(eaBusinessApplyChangeList != null && eaBusinessApplyChangeList.size() > 0){
			EaBusinessApplyChange eaBusinessApplyChange = eaBusinessApplyChangeList.get(0);
			//组织名称（中文）
			String companyCodeIdName = changeIdToNameUtil.getCompanyNameById(eaBusinessApplyChange.getCompanyCodeId());
			eaBusinessApplyChange.setCompanyCodeIdName(companyCodeIdName);
			//部门名称（中文）
			String deptIdName = changeIdToNameUtil.getNameByDepartId(eaBusinessApplyChange.getDeptId());
			eaBusinessApplyChange.setDeptIdName(deptIdName);
			//发票名称 （中文）
			String typeName = changeIdToNameUtil.getTypeIdById(eaBusinessApplyChange.getType());
			eaBusinessApplyChange.setTypeName(typeName);

			//交通工具
			String trafficTools = eaBusinessApplyChange.getTrafficTools();
			String trafficToolsName = "";
			if(trafficTools != null){
				if(trafficTools.contains(",")){
					String[] trafficToolss = trafficTools.split(",");
					for (int i = 0; i < trafficToolss.length; i++) {
						if(trafficToolsName.equals("")){
							Map<String,Object> params = new HashMap<String,Object>();
							params.put("item_value",trafficToolss[i]);
							params.put("dict_id","1410511721257771009");
							List<EaSysDictItem> eaSysDictItemList = eaSysDictItemMapper.selectByMap(params);
							if(eaSysDictItemList != null && eaSysDictItemList.size() > 0){
								trafficToolsName = eaSysDictItemList.get(0).getItemText();
							}
						}else{
							Map<String,Object> params = new HashMap<String,Object>();
							params.put("item_value",trafficToolss[i]);
							params.put("dict_id","1410511721257771009");
							List<EaSysDictItem> eaSysDictItemList = eaSysDictItemMapper.selectByMap(params);
							if(eaSysDictItemList != null && eaSysDictItemList.size() > 0){
								trafficToolsName = trafficToolsName + "," + eaSysDictItemList.get(0).getItemText();
							}
						}
					}
				}else{
					Map<String,Object> params = new HashMap<String,Object>();
					params.put("item_value",trafficTools);
					params.put("dict_id","1410511721257771009");
					List<EaSysDictItem> eaSysDictItemList = eaSysDictItemMapper.selectByMap(params);
					if(eaSysDictItemList != null && eaSysDictItemList.size() > 0){
						trafficToolsName = eaSysDictItemList.get(0).getItemText();
					}
				}
			}
			eaBusinessApplyChange.setTrafficToolsName(trafficToolsName);


			//将实体数据存到map
			returnMap = JSON.parseObject(JSON.toJSONString(eaBusinessApplyChange, SerializerFeature.WriteMapNullValue), Map.class);

			//格式化时间
			Long createTime = returnMap.get("createTime") != null ? Long.parseLong(returnMap.get("createTime").toString()) : null;
			String createTimeStr = DateFormatUtil.formatDateyyMMddHHmmss(createTime);
			returnMap.put("createTime",createTimeStr);

			//格式化时间
			Long updateTime = returnMap.get("updateTime") != null ? Long.parseLong(returnMap.get("updateTime").toString()) : null;
			String updateTimeStr = DateFormatUtil.formatDateyyMMddHHmmss(updateTime);
			returnMap.put("updateTime",updateTimeStr);

			//更新时间-用于校验时效性
			returnMap.put("time",updateTimeStr);

			//格式化时间
			Long billDate = returnMap.get("billDate") != null ? Long.parseLong(returnMap.get("billDate").toString()) : null;
			String billDateStr = DateFormatUtil.formatDateyyMMdd(billDate);
			returnMap.put("billDate",billDateStr);

			//格式化时间
			Long startDate = returnMap.get("startDate") != null ? Long.parseLong(returnMap.get("startDate").toString()) : null;
			String startDateStr = DateFormatUtil.formatDateyyMMdd(startDate);
			returnMap.put("startDate",startDateStr);

			//格式化时间
			Long endDate = returnMap.get("endDate") != null ? Long.parseLong(returnMap.get("endDate").toString()) : null;
			String endDateStr = DateFormatUtil.formatDateyyMMdd(endDate);
			returnMap.put("endDate",endDateStr);

			//添加图片
			String picture = returnMap.get("picture") != null ? returnMap.get("picture").toString() : "";
			List<Map<String,Object>> fileList = GetUrlUtil.getUrl(picture,fileurl);

			//添加图片fileList
			returnMap.put("fileList",fileList);

			//添加出行人员明细
			Map<String,Object> eaBusinessPerDtlMap = new HashMap<String,Object>();
			eaBusinessPerDtlMap.put("ea_business_apply_id",id);
			List<EaBusinessPerDtl> eaBusinessPerDtlList = eaBusinessPerDtlMapper.selectByMap(eaBusinessPerDtlMap);
			List<Map<String,Object>> eaBusinessPerDtlMapList = new ArrayList<Map<String,Object>>();
			if(eaBusinessPerDtlList != null && eaBusinessPerDtlList.size() > 0){
				for (int i = 0; i < eaBusinessPerDtlList.size(); i++) {
					Map<String,Object> eaBusinessPerDtlMap2 = JSON.parseObject(JSON.toJSONString(eaBusinessPerDtlList.get(i), SerializerFeature.WriteMapNullValue), Map.class);
					//格式化时间
					Long createTime1 = eaBusinessPerDtlMap2.get("createTime") != null ? Long.parseLong(eaBusinessPerDtlMap2.get("createTime").toString()) : null;
					String createTime1Str = DateFormatUtil.formatDateyyMMddHHmmss(createTime1);
					eaBusinessPerDtlMap2.put("createTime",createTime1Str);

					//格式化时间
					Long updateTime1 = eaBusinessPerDtlMap2.get("updateTime") != null ? Long.parseLong(eaBusinessPerDtlMap2.get("updateTime").toString()) : null;
					String updateTime1Str = DateFormatUtil.formatDateyyMMddHHmmss(updateTime1);
					eaBusinessPerDtlMap2.put("updateTime",updateTime1Str);

					//格式化时间-开始时间
					Long startDateDtl = eaBusinessPerDtlMap2.get("startDateDtl") != null ? Long.parseLong(eaBusinessPerDtlMap2.get("startDateDtl").toString()) : null;
					String startDateDtlStr = DateFormatUtil.formatDateyyMMdd(startDateDtl);
					eaBusinessPerDtlMap2.put("startDateDtl",startDateDtlStr);

					//格式化时间-结束时间
					Long endDateDtl = eaBusinessPerDtlMap2.get("endDateDtl") != null ? Long.parseLong(eaBusinessPerDtlMap2.get("endDateDtl").toString()) : null;
					String endDateDtlStr = DateFormatUtil.formatDateyyMMdd(endDateDtl);
					eaBusinessPerDtlMap2.put("endDateDtl",endDateDtlStr);

					//格式化人员名称
//					String personnel2 = EmptyUtil.checkNull(eaBusinessPerDtlMap2.get("personnel"));
//					String personnel2Name = changeIdToNameUtil.getPsnNameById(personnel2);
//					eaBusinessPerDtlMap2.put("personnelName",personnel2Name);

					//添加借款单明细
					eaBusinessPerDtlMapList.add(eaBusinessPerDtlMap2);
				}
			}

			//将明细信息加入返回节点
			returnMap.put("eaBusinessPerDtlList",eaBusinessPerDtlMapList);
		}
		return returnMap;
	}

	@Override
	@Transactional
	public Map<String, Object> audit(EaTaskVo eaTaskVo) {
		Map<String,Object> returnMap = new HashMap<String,Object>();
		String id = EmptyUtil.checkNull(eaTaskVo.getId());   //单据id
//        String auditManId = EmptyUtil.checkNull(eaTaskVo.getAuditManId());   //审批人id
//        String auditMan = EmptyUtil.checkNull(eaTaskVo.getAuditMan());   //审批人
		String auditComment = EmptyUtil.checkNull(eaTaskVo.getAuditComment());   //审批意见
		String status = EmptyUtil.checkNull(eaTaskVo.getStatus());   //单据状态:2通过,3退回

		//获取出差申请变更单-更新状态
		EaBusinessApplyChange eaBusinessApplyChange = eaBusinessApplyChangeMapper.selectById(id);
		eaBusinessApplyChange.setState(Integer.parseInt(status));
		eaBusinessApplyChangeMapper.updateById(eaBusinessApplyChange);

		//当出差申请变更单审核通过后,要将修改信息同步到相应的出差申请单
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("bill_no",eaBusinessApplyChange.getEaBusinessApplyNo());
		List<EaBusinessApply> eaBusinessApplyList = eaBusinessApplyMapper.selectByMap(params);
		if(eaBusinessApplyList != null && eaBusinessApplyList.size() > 0){
			EaBusinessApply eaBusinessApply = eaBusinessApplyList.get(0);
			eaBusinessApply.setIfChanged(0);   //是否变更状态
			if(status.equals("2")){
				eaBusinessApply.setRegion(eaBusinessApplyChange.getRegion());    //目的地
				eaBusinessApply.setStartDate(eaBusinessApplyChange.getStartDate());    //开始时间
				eaBusinessApply.setEndDate(eaBusinessApplyChange.getEndDate());    //结束时间
				eaBusinessApply.setIfCompanyCar(eaBusinessApplyChange.getIsCompanyCar());    //是否公司配车
				eaBusinessApply.setPlateNumber(eaBusinessApplyChange.getPlateNumber());    //车牌号
				eaBusinessApply.setDriver(eaBusinessApplyChange.getDriver());    //司机
				eaBusinessApply.setCostCompCodeId(eaBusinessApplyChange.getCostCompCodeId());    //费用承担单位
				eaBusinessApply.setCostDept(eaBusinessApplyChange.getCostDept());    //费用承担部门
//				eaBusinessApply.setPersonnel(eaBusinessApplyChange.getPersonnel());    //出行人员
				eaBusinessApply.setExplainStr(eaBusinessApplyChange.getCause());    //事由

				//删除出行人员明细
				Map<String,Object> personnelMap = new HashMap<String,Object>();
				personnelMap.put("ea_business_apply_id",eaBusinessApply.getId());
				eaBusinessPerDtlMapper.deleteByMap(personnelMap);

				Map<String,Object> params2 = new HashMap<String,Object>();
				params2.put("ea_business_apply_id",eaBusinessApplyChange.getId());
				List<EaBusinessPerDtl> eaBusinessPerDtlList = eaBusinessPerDtlMapper.selectByMap(params2);
				if(eaBusinessPerDtlList != null && eaBusinessPerDtlList.size() > 0){
					for (int i = 0; i < eaBusinessPerDtlList.size(); i++) {
						EaBusinessPerDtl eaBusinessPerDtl = eaBusinessPerDtlList.get(i);
						EaBusinessPerDtl eaBusinessPerDtl2 = new EaBusinessPerDtl();
						BeanUtils.copyProperties(eaBusinessPerDtl,eaBusinessPerDtl2);

						//插入新的出差申请单明细
						eaBusinessPerDtl2.setId(Uuid32Util.getUUid());
						eaBusinessPerDtl2.setEaBusinessApplyId(eaBusinessApply.getId());
						eaBusinessPerDtlMapper.insert(eaBusinessPerDtl2);
					}
				}
			}
			eaBusinessApplyMapper.updateById(eaBusinessApply);
		}

		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("form_id",eaBusinessApplyChange.getId());
		List<EaTask> eaTaskList = eaTaskMapper.selectByMap(paramsMap);
		if(eaTaskList != null && eaTaskList.size() > 0){
			EaTask eaTask = eaTaskList.get(0);
			//eaTask.setAuditManId(auditManId);
			//eaTask.setAuditMan(auditMan);
			eaTask.setAuditComment(auditComment);
			eaTask.setStatus(status);

			try {
				if(status.equals("3") || status.equals("2")){
					eaTask.setFinishTime(new Date());   //完成时间
				}
				eaTask.setAuditTime(new Date());    //审批时间
				eaTaskMapper.updateById(eaTask);

				returnMap.put("code","0");
				returnMap.put("message","审核成功!");
				return returnMap;
			}catch (Exception e){
				returnMap.put("code","-1");
				returnMap.put("message","系统异常,审核失败!");
				return returnMap;
			}
		}else{
			returnMap.put("code","-1");
			returnMap.put("message","待办不存在!");
			return returnMap;
		}
	}

}
